const express = require('express');
const router = express.Router();
const { get, post } = require('../util/http');

// 获取是否开启监控和保存天数
router.all('/GetMonitorStatus', (req, res) => {
    post('GetMonitorStatus', {
        type: '-1'
    }).then(response => {
        res.send(response);
    }).catch((e) => {
        res.send(e)
    })
})

// 开启关闭系统监控
router.all('/SetMonitorStatus', (req, res) => {
    let params = req.body;
    if (typeof params.type == 'undefined' || (params.type != '0' && params.type != '1')) {
        res.json({ status: false, msg: "type参数错误" });
        return;
    }
    if (typeof params.day == 'undefined' || isNaN(params.day) || params.day <= 0) {
        res.json({ status: false, msg: "day参数错误" });
        return;
    }
    // 0=>关闭 1=>开启
    post('SetMonitorStatus', {
        type: params.type,
        day: params.day
    }).then(response => {
        res.send(response);
    }).catch((e) => {
        res.send(e)
    })
})

// 获取平均负载
router.all('/GetLoadAverage', (req, res) => {
    let params = req.body;
    if (typeof params.start == 'undefined') {
        res.json({ status: false, msg: "start参数错误" });
        return;
    }
    if (typeof params.end == 'undefined') {
        res.json({ status: false, msg: "end参数错误" });
        return;
    }
    post('GetLoadAverage', {
        start: params.start,
        end: params.end
    }).then(response => {
        res.send(response);
    }).catch((e) => {
        res.send(e)
    })
})

// 获取cpu内存统计
router.all('/GetCpuIo', (req, res) => {
    let params = req.body;
    if (typeof params.start == 'undefined') {
        res.json({ status: false, msg: "start参数错误" });
        return;
    }
    if (typeof params.end == 'undefined') {
        res.json({ status: false, msg: "end参数错误" });
        return;
    }
    post('GetCpuIo', {
        start: params.start,
        end: params.end
    }).then(response => {
        res.send(response);
    }).catch((e) => {
        res.send(e)
    })
})

//获取磁盘统计
router.all('/GetDiskIo', (req, res) => {
    let params = req.body;
    if (typeof params.start == 'undefined') {
        res.json({ status: false, msg: "start参数错误" });
        return;
    }
    if (typeof params.end == 'undefined') {
        res.json({ status: false, msg: "end参数错误" });
        return;
    }
    post('GetDiskIo', {
        start: params.start,
        end: params.end
    }).then(response => {
        res.send(response);
    }).catch((e) => {
        res.send(e)
    })
})

//获取网络统计
router.all('/GetNetWorkIo', (req, res) => {
    let params = req.body;
    if (typeof params.start == 'undefined') {
        res.json({ status: false, msg: "start参数错误" });
        return;
    }
    if (typeof params.end == 'undefined') {
        res.json({ status: false, msg: "end参数错误" });
        return;
    }
    post('GetNetWorkIo', {
        start: params.start,
        end: params.end
    }).then(response => {
        res.send(response);
    }).catch((e) => {
        res.send(e)
    })
})

// 清空监控数据
router.all('/DelMonitorData', (req, res) => {
    post('DelMonitorData', { type: 'del' }).then(response => {
        res.send(response);
    }).catch((e) => {
        res.send(e)
    })
})

module.exports = router;